package com.yingxue.lesson.service.impl;

import com.yingxue.lesson.entity.User;
import com.yingxue.lesson.mapper.SysUserMapper;
import com.yingxue.lesson.service.UserCacheService;
import com.yingxue.lesson.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author gusheng
 * @Date 2021/1/6 16:06
 * @ClassName 类名称
 * @Description 类描述
 * @Version 3.4
 */
@Service
@CacheConfig(cacheNames = { "userCache" })
public class UserCacheServiceImpl implements UserCacheService {

    public static final String CACHE_KEY_USER = "user:";

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    @CachePut(key = "#user.id")
    public User createUser(User user) {
         this.sysUserMapper.insertSelectiveUser(user);
         return this.sysUserMapper.selectByPrimaryKey2(user.getId());
    }

    @Override
    @CachePut(key = "#user.id")
    public User updateUser(User user) {
        int i = this.sysUserMapper.updateByPrimaryKeyUser(user);
        return this.sysUserMapper.selectByPrimaryKey2(user.getId());
    }

    @Override
    @Cacheable(key="#id")
    public User findUserById(int id) {
         return this.sysUserMapper.selectByPrimaryKey2(id);
    }

    @Override
    @CacheEvict(key = "#id")
    public void deleteUser(int id) {
        this.sysUserMapper.removeUserByid(id);
    }

}
